package com.one.blocks.common.response;

import com.github.pagehelper.Page;
import com.one.blocks.common.enums.CodeMsgEnum;
import com.one.blocks.common.enums.ResponseEnum;
import lombok.experimental.UtilityClass;

import java.util.List;

/**
 * @author linfuxin Created on 2022-02-14 10:43:18
 */
@UtilityClass
public class ResponseHelper {
    public static WebResponse<Void> success() {
        return success(null);
    }

    public static <T> WebResponse<T> success(T data) {
        return new WebResponse<>(ResponseEnum.SUCCEED.getCode(), null, data);
    }

    public static <T> WebPageResponse<T> successPage(Page<?> page, List<T> data) {
        return WebPageResponse.<T>builder()
                .code(ResponseEnum.SUCCEED.getCode())
                .msg(null)
                .data(data)
                .total(page.getTotal())
                .build();
    }

    public static <T> WebPageResponse<T> successPage() {
        return WebPageResponse.<T>builder()
                .code(ResponseEnum.SUCCEED.getCode())
                .msg(null)
                .data(null)
                .total(0)
                .build();
    }

    public static <T> WebResponse<T> fail(CodeMsgEnum errorEnum) {
        return new WebResponse<>(errorEnum.getCode(), errorEnum.getMsg(), null);
    }

    public static <T> WebResponse<T> fail(int code, String errorMsg) {
        return new WebResponse<>(code, errorMsg, null);
    }
}
